from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject


MAX_PHOTO_SIZE = 10


class Photo():
  def __init__(self, photopath):
    self.photopath = photopath
    self.__width = None
    self.__height = None
    self.__texture = None
    self.__panel = None
    self.__node_path = None


  ''' Lazy loading getter for the texture '''
  def __createTexture(self):
    image = PNMImage()
    image.read(Filename(self.photopath))
    self.__texture = Texture()
    self.__texture.load(image)
    self.__width = image.getXSize()
    self.__height = image.getYSize()


  def getTexture(self):
    if not self.__texture:
      self.__createTexture()
    return self.__texture


  ''' Lazy loading getter for the panel '''
  def __createPanel(self):
    if not self.__width or not self.__height:
      self.__createTexture()
    ratio = float(self.__width) / float(self.__height)

    if ratio > 1:
      width, height = (MAX_PHOTO_SIZE, MAX_PHOTO_SIZE / ratio)
    else:
      width, height = (MAX_PHOTO_SIZE * ratio, MAX_PHOTO_SIZE)


    '''Loading grid'''
    self.__node_path.setTexture(self.getTexture())
    self.__panel = loader.loadModel("models/grid.egg")
    self.__panel.reparentTo(self.__node_path)
    self.__panel.setScale(Vec3(width,1,height))
    center = self.__panel.getBounds().getCenter()
    self.__panel.setPos( -center.getX(), -center.getY(), -center.getZ() )


  ''' Lazy loading getter for the node path '''
  def __createNodePath(self):
    self.__node_path = NodePath('photo')


  def getNodePath(self):
    if not self.__node_path:
      self.__createNodePath()
      self.__createPanel()

    return self.__node_path


class PhotoWall(DirectObject):
  def __init__(self, columns, rows, padding=1):
    self.columns = columns
    self.rows = rows
    self.padding = padding
    self.photos = []
    self.__node_path = None
    self.__panel = None
    self.__unitW = None
    self.__unitH = None
    self.npImage = None


  def __createPanel(self):
    w = self.columns * MAX_PHOTO_SIZE + ((1+self.columns) * self.padding)
    h = self.rows * MAX_PHOTO_SIZE + ((1+self.rows) * self.padding)
    self.__unitW = w/self.columns
    self.__unitH = h/self.rows
    self.__panel = loader.loadModel("models/grid.egg")
    self.__panel.setScale(Vec3(w, 1, h))
    self.__panel.setTwoSided(True)

    #Adding panel as a child under node_path
    self.__panel.reparentTo(self.__node_path)


  ''' Lazy loading getter for the node path '''
  def __createNodePath(self):
    self.__node_path = NodePath('photowall')


  def getNodePath(self):
    if not self.__node_path:
      self.__createNodePath()
      self.__createPanel() #create panel and calc coordinates
      tex = loader.loadTexture('textures/wall.png')
      self.__node_path.setTexture(tex)
    return self.__node_path


  ''' Add photos in various ways'''
  def addPhoto(self, photo):
    idx = len(self.photos)
    self.photos.append(photo)
    pnp = photo.getNodePath()
    pnp.setTag("pw", "1")
    #pnp.setTag("pid", photo.photopath)
    wnp = self.getNodePath()
    pnp.reparentTo(wnp)

    # Figure out the row and column indices
    r = idx/self.columns
    c = idx%self.columns

    # Calculate relative position
    x = self.__unitW * (c + 0.5)
    y = self.__unitH * (self.rows - r - 0.5)
    pnp.setPos(x,-1,y)
    return pnp


  def addPhotoPath(self, photopath):
    p = Photo(photopath)
    return self.addPhoto(p)